package com.bnzj.cloud.business.core.persistence.entity;

import com.bnzj.core.persistence.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Getter
@Setter
@ToString(exclude = {"children", "parent"}, callSuper = true)
@Entity
@Table(name = "bnz_sys_resource")
@Schema(description = "系统资源")
public class SysResource extends BaseEntity {
    private static final long serialVersionUID = 2353440166698313329L;

    @Schema(description = "父系统资源ID")
    private Long pid;//父ID

    @Schema(description = "资源名称")
    @NotNull(message = "资源名称不能为空！")
    private String name;//资源名称

    @Schema(description = "css样式名")
    private String cssClass;//css样式名

    @Schema(description = "资源类型(0页面菜单, 1页面按钮, 2工具条按钮, 3行操作, 4弹出页面)")
    @NotNull(message = "资源类型不能为空！")
    private Integer type;//资源类型(0页面菜单, 1页面按钮, 2工具条按钮, 3行操作, 4弹出页面)

    @Schema(description = "资源路径")
    private String url;//资源路径

    @Schema(description = "方法名称")
    private String methodName;//方法名称

    @Schema(description = "次序")
    @NotNull(message = "资源排序序号不能为空！")
    private Short sequence;//次序

    @Schema(description = "路径索引")
    private String pathIndex;//路径索引

    @Schema(description = "资源说明")
    private String remark;//资源说明

    @OneToMany(mappedBy = "pid", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
//    @JsonIgnore
    private List<SysResource> children = new ArrayList<SysResource>();

    @ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "pid", insertable = false, updatable = false)
    @JsonIgnore
    private SysResource parent;


    /**
     * 后台资源
     */
    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "bnz_sys_resource_rel_meta", joinColumns = {@JoinColumn(name = "resource_id")}, inverseJoinColumns = {@JoinColumn(name = "meta_id")})
    @Where(clause = "enabled =1")
    private Set<SysResourceMeta> metaSet;


    @OneToMany(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY, mappedBy = "resource")
    @Schema(description = "关联角色")
    private List<SysRoleResource> sysRoleResources = new ArrayList<SysRoleResource>();
    private static final SysResource root = new SysResource();

    static {
        root.setId(0L);
        root.setName("系统");
    }

    public static SysResource getRoot() {
        return root;
    }
}
